#!/bin/bash

BASE_PATH=`pwd`
DEVOPS_CONFIG=$BASE_PATH/devops.ini

DEFAULT_LOCATION=`awk -F '=' '/\[Common\]/{a=1}a==1&&$1~/DEFAULT_LOCATION/{print $2;exit}' $DEVOPS_CONFIG`

MYSQL_URL=`awk -F '=' '/\[MySQL\]/{a=1}a==1&&$1~/MYSQL_URL/{print $2;exit}' $DEVOPS_CONFIG`
PASSWORD=`awk -F '=' '/\[MySQL\]/{a=1}a==1&&$1~/PASSWORD/{print $2;exit}' $DEVOPS_CONFIG`

SERVER_IMAGE=`awk -F '=' '/\[Server\]/{a=1}a==1&&$1~/SERVER_IMAGE/{print $2;exit}' $DEVOPS_CONFIG`
SERVER_PORT=`awk -F '=' '/\[Server\]/{a=1}a==1&&$1~/SERVER_PORT/{print $2;exit}' $DEVOPS_CONFIG`


CLIENT_IMAGE=`awk -F '=' '/\[Client\]/{a=1}a==1&&$1~/CLIENT_IMAGE/{print $2;exit}' $DEVOPS_CONFIG`
CLIENT_PORT=`awk -F '=' '/\[Client\]/{a=1}a==1&&$1~/CLIENT_PORT/{print $2;exit}' $DEVOPS_CONFIG`
DEVOPS_SERVER=`awk -F '=' '/\[Client\]/{a=1}a==1&&$1~/DEVOPS_SERVER/{print $2;exit}' $DEVOPS_CONFIG`


export DEFAULT_LOCATION=$DEFAULT_LOCATION
export MYSQL_URL=$MYSQL_URL
export PASSWORD=$PASSWORD
export SERVER_IMAGE=$SERVER_IMAGE
export SERVER_PORT=$SERVER_PORT
export CURRENT_LOCATION=$BASE_PATH
export CLIENT_IMAGE=$CLIENT_IMAGE
export CLIENT_PORT=$CLIENT_PORT
export DEVOPS_SERVER=$DEVOPS_SERVER

envsubst '$MYSQL_URL,$PASSWORD' < devops.yml.template > devops.yml

envsubst '$DEFAULT_LOCATION,$SERVER_IMAGE,$SERVER_PORT,$CURRENT_LOCATION,$CLIENT_IMAGE,$CLIENT_PORT,$DEVOPS_SERVER' < devops-compose.yaml.template > devops-compose.yaml

docker-compose -f devops-compose.yaml up -d 
